package com.ycsj.goldmedalnewconcept.common.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.image.ImageUtil;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.AboutUsInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.common.util.LpUtils;
import com.ycsj.goldmedalnewconcept.master.activity.MasterEditAboutUsActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 通用的关于我们界面
 * Created by Administrator on 2019/5/5 0005.
 */

public class AboutUsActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lv_edit)
    ImageView lvEdit;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_email)
    TextView tvEmail;
    @BindView(R.id.tv_tel)
    TextView tvTel;
    @BindView(R.id.lv_code)
    ImageView lvCode;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    //学校的相关信息
    private String app_content, app_url, app_email, app_phone;


    //页面更新的标识
    public static final int UPDATE = 0x870;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_about_us;
    }

    @Override
    protected void init() {
        initStateBar();

        //初始化标题栏
        initTitle();

        //请求服务器
        http();
    }


    /**
     * 初始化标题栏
     */
    private void initTitle() {
        tvTitle.setText("关于我们");

        if ("3".equals(AccountUtils.getRole(this))) {
            //校长端
            RelativeLayout.LayoutParams lp = LpUtils.getRelatLp(UiUtils.dp2px(36), UiUtils.dp2px(36));
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            lvEdit.setLayoutParams(lp);
            lvEdit.setImageResource(R.drawable.edit);
            lvEdit.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 请求服务器的方法
     */
    private void http() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "1");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("school", AccountUtils.getSchoolId(this));
        map.put("platform", "2");
        LogUtils.iTag("test", HttpAddress.ABOUTUS + "?platform=2&type=1&acc=" + AccountUtils.getAccount(this)
                + "&role=" + AccountUtils.getRole(this) + "&school=" + AccountUtils.getSchoolId(this));
        HttpUtils.post(HttpAddress.ABOUTUS, map, this, new HttpCallBack<AboutUsInfo>() {
            @Override
            public void onSuccess(AboutUsInfo aboutUsInfo) {
                rlLoading.setVisibility(View.GONE);
                llContent.setVisibility(View.VISIBLE);
                //设置页面数据
                setPageData(aboutUsInfo);
            }

            @Override
            public void onError(String e) {
                rlLoading.setVisibility(View.GONE);
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });
    }

    /**
     * 设置页面数据
     *
     * @param info
     */
    public void setPageData(AboutUsInfo info) {
        app_content = info.getApp_content();
        app_email = info.getApp_email();
        app_phone = info.getApp_phone();
        app_url = info.getApp_url();
        //内容
        if (!TextUtils.isEmpty(info.getApp_content())) {
            tvContent.setText(info.getApp_content().replace("\\n", "\n"));

        } else {
            tvContent.setText(getResources().getString(R.string.ycsjcontent).replace("\\n", "\n"));
        }

        //邮箱
        if (!TextUtils.isEmpty(info.getApp_email())) {
            tvEmail.setText(info.getApp_email());
        } else {
            tvEmail.setText(getResources().getString(R.string.ycsjemail));
        }

        //电话
        if (!TextUtils.isEmpty(info.getApp_phone())) {
            tvTel.setText(info.getApp_phone());
        } else {
            tvTel.setText(getResources().getString(R.string.ycsjtel));
        }

        //二维码
        if (!TextUtils.isEmpty(info.getApp_qrcode())) {
            ImageUtil.displayImage(this, lvCode, info.getApp_qrcode());
        } else {
            lvCode.setImageResource(R.drawable.yingcaishijia);
        }

        //名称
        if (!TextUtils.isEmpty(info.getSchool_name())) {
            tvName.setText(info.getSchool_name());
        } else {
            tvName.setText(getResources().getString(R.string.ycsjname));
        }

    }

    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_back, R.id.lv_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.lv_edit:
                //校长端编辑关于我们
                MasterEditAboutUsActivity.goActivity(this, app_content, app_url, app_email, app_phone);
                break;
        }
    }

    /**
     * 事件处理
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        if (null != busInfo && UPDATE == busInfo.type) {
            http();
        }
    }
}
